#!/bin/sh
# PCP QA Test No. 892
# Exercise daemon local-only access in IPv6 mode.
#
# Copyright (c) 2015 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

$sudo rm -rf $tmp.* $seq.full

_get_libpcp_config
$ipv6 || _notrun "IPv6 support is not available"
host=`hostname`
[ -z "$host" ] && _notrun "Cannot discover local host name"
_host_to_ipv6addrs $host | tee -a $seq.full >$tmp.tmp
addr=`head -1 $tmp.tmp`
[ -z "$addr" ] && _notrun "Cannot translate hostname ($host) to IPv6 address"
ipv6local=`_ipv6_localhost`

if [ -f /etc/lsb-release ]
then
    if grep -q 'DISTRIB_ID=Ubuntu' /etc/lsb-release
    then
	if grep -q 'DISTRIB_RELEASE=12.04' /etc/lsb-release
	then
	    # getaddrinfo() for Ubuntu 12.04 does not seem to be able
	    # to handle the IPv6 name localhost6 (or similar) ... at least
	    # not on vm00 in the QA farm ... so don't run the test (it gets
	    # exercised in lots of other places) because this would not
	    # appear to be a PCP-related issue
	    #
	    _notrun "Ubuntu + 12.04 + getaddrinfo() == bad karma"
	    # NOTREACHED
	fi
    fi
fi

status=1	# failure is the default!

_cleanup()
{
    # ensure we do not leave local-only settings enabled
    _restore_config $PCP_SYSCONFIG_DIR/pmcd

    _service pcp restart 2>&1 | _filter_pcp_stop | _filter_pcp_start
    _wait_for_pmcd
    _wait_for_pmlogger

    rm -rf $tmp.*
}

_filter()
{
    tee -a $seq.full | \
    sed \
	-e "s/$host/HOSTNAME/g" \
	-e "s/$addr/IPV6ADDR/g" \
    #end
}
trap "_cleanup; exit \$status" 0 1 2 3 15

# don't need to regenerate config.default with pmlogconf
#
export PMLOGGER_CHECK_SKIP_LOGCONF=yes

# don't need to regenerate config.default with pmlogconf
#
export PMLOGGER_CHECK_SKIP_LOGCONF=yes

# real QA test starts here
echo "Using IPv6 addr=\"$addr\" ipv6local=\"$ipv6local\"" >>$seq.full

_save_config $PCP_SYSCONFIG_DIR/pmcd

echo; echo "== checking loop pmcd access, expecting success"
pminfo -f -h "pcp://$ipv6local" pmcd.feature.local 2>&1 | _filter

echo; echo "== checking ipv6 pmcd access, expecting success"
pminfo -f -h "pcp://[$addr]" pmcd.feature.local 2>&1 | _filter

echo; echo "== changing default setup to local host only"; echo
echo "# Dummy entry added by PCP QA test $seq" > $tmp.local
echo PMCD_LOCAL=1 >> $tmp.local
$sudo cp $tmp.local $PCP_SYSCONFIG_DIR/pmcd

_service pmcd restart 2>&1 | _filter_pcp_stop | _filter_pcp_start
_wait_for_pmcd
_wait_for_pmlogger

echo; echo "== checking loop pmcd access, expecting success"
pminfo -f -h "pcp://$ipv6local" pmcd.feature.local 2>&1 | _filter

echo; echo "== checking bad pmcd access, expect no values"
pminfo -f -h "pcp://[$addr]" pmcd.feature.local 2>&1 | _filter

# success, all done
status=0
exit
